import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:image/image.dart' as img;

/// 图像处理工具类
class ImageUtils {
  /// 生成纯色图片
  static Future<Uint8List?> createImage(Color color, int width, int height, String format) async {
    try {
      // 创建一个指定大小的图片
      final image = img.Image(width: width, height: height);

      // 将颜色填充到图片
      img.fill(image, color: img.ColorRgb8(color.red, color.green, color.blue));

      // 根据格式编码图片
      List<int>? encodedImage;
      switch (format.toLowerCase()) {
        case 'png':
          encodedImage = img.encodePng(image);
          break;
        case 'jpg':
          encodedImage = img.encodeJpg(image);
          break;
        case 'bmp':
          encodedImage = img.encodeBmp(image);
          break;
      }

      return encodedImage != null ? Uint8List.fromList(encodedImage) : null;
    } catch (e) {
      debugPrint('Error creating image: $e');
      return null;
    }
  }
}